/**
 * Created with IntelliJ IEDA.
 * Description:
 * User:86186
 * Date:2023-04-23
 * Time:19:57
 */

/**
 * BM17 二分查找-l
 */

public class search {
    public int search (int[] nums, int target) {
        // write code here
//        for (int i = 0; i < nums.length; i++) {
//            if (nums[i] == target){
//                return i;
//            }
//        }
//        return -1;
        int left = 0;
        int right = nums.length - 1;
        while (left <= right){
            int mid = (left + right) / 2;
            if (nums[mid] > target){
                right--;
            }else if (nums[mid] < target){
                left++;
            }else if (nums[mid] ==target){
                return mid;
            }
        }
        return -1;
    }
}
